package com.resto.scm.web.service.impl;

import cn.restoplus.rpc.server.RpcService;
import com.resto.scm.web.dto.DocPmsPoDetailDo;
import com.resto.scm.web.dto.DocPmsPoHeaderDetailDo;
import com.resto.scm.web.manager.DocPmsPoDetailManager;
import com.resto.scm.web.manager.DocPmsPoHeaderManager;
import com.resto.scm.web.model.DocPmsPoDetail;
import com.resto.scm.web.model.DocPmsPoHeader;
import com.resto.scm.web.service.DocPmsPoHeaderService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@RpcService
public class DocPmsPoHeaderServiceImpl implements DocPmsPoHeaderService {

    @Resource
    private DocPmsPoHeaderManager docPmsPoHeaderManager;
    @Resource
    private DocPmsPoDetailManager docPmsPoDetailManager;

	private DocPmsPoDetailDo generateDocPmsPoHeaderDetailDo(DocPmsPoHeaderDetailDo docPmsPoHeaderDetailDo) {
		DocPmsPoDetailDo docPmsPoDetail =  new DocPmsPoDetailDo();
		docPmsPoDetail.setActQty(docPmsPoHeaderDetailDo.getActQty());
		docPmsPoDetail.setCategoryOneName(docPmsPoHeaderDetailDo.getCategoryOneName());
		docPmsPoDetail.setCategoryTwoName(docPmsPoHeaderDetailDo.getCategoryTwoName());
		docPmsPoDetail.setCategoryThirdName(docPmsPoHeaderDetailDo.getCategoryThirdName());
		docPmsPoDetail.setCityName(docPmsPoHeaderDetailDo.getCityName());
		docPmsPoDetail.setProvinceName(docPmsPoHeaderDetailDo.getProvinceName());
		docPmsPoDetail.setDistrictName(docPmsPoHeaderDetailDo.getDistrictName());
		docPmsPoDetail.setMaterialCode(docPmsPoHeaderDetailDo.getMaterialCode());
		docPmsPoDetail.setMaterialType(docPmsPoHeaderDetailDo.getMaterialType());
		docPmsPoDetail.setMaterialName(docPmsPoHeaderDetailDo.getMaterialName());
		docPmsPoDetail.setMeasureUnit(docPmsPoHeaderDetailDo.getMeasureUnit());
		docPmsPoDetail.setNote(docPmsPoHeaderDetailDo.getNote());
		docPmsPoDetail.setSpecName(docPmsPoHeaderDetailDo.getSpecName());
		docPmsPoDetail.setTopContact(docPmsPoHeaderDetailDo.getTopContact());
		docPmsPoDetail.setTopEmail(docPmsPoHeaderDetailDo.getTopEmail());
		docPmsPoDetail.setTopMobile(docPmsPoHeaderDetailDo.getTopMobile());

		docPmsPoDetail.setPurchaseMoney(docPmsPoHeaderDetailDo.getPurchaseMoney());
		docPmsPoDetail.setPurchaseTaxMoney(docPmsPoHeaderDetailDo.getPurchaseTaxMoney());
		docPmsPoDetail.setPurchaseRealMoney(docPmsPoHeaderDetailDo.getPurchaseRealMoney());
		docPmsPoDetail.setPurchaseRealTaxMoney(docPmsPoHeaderDetailDo.getPurchaseRealTaxMoney());
		docPmsPoDetail.setPurchaseRealMoney(docPmsPoHeaderDetailDo.getReceivedMoney());
		docPmsPoDetail.setReceivedTaxMoney(docPmsPoHeaderDetailDo.getReceivedTaxMoney());
		return docPmsPoDetail;
	}



	@Override
	public List<DocPmsPoHeaderDetailDo> queryJoin4Page(String shopId,String shopName) {
		List<DocPmsPoHeaderDetailDo> pmsPoHeaderDetailDos = docPmsPoHeaderManager.queryJoin4Page(shopId);

		Map<Long, List<DocPmsPoHeaderDetailDo>> collects = pmsPoHeaderDetailDos.stream().collect(Collectors.groupingBy(DocPmsPoHeaderDetailDo::getId));

		List<DocPmsPoHeaderDetailDo> returnDocPmsPoHeaderDetailDos = new ArrayList<>(collects.size());

		for (Map.Entry<Long,List<DocPmsPoHeaderDetailDo>> each:collects.entrySet()) {
			//组装头部
			List<DocPmsPoHeaderDetailDo> values = each.getValue();
			DocPmsPoHeaderDetailDo pmsPoHeaderDetailDo = values.get(0);
			//组装detail
			List<DocPmsPoDetailDo> docPmsPoDetails = new ArrayList<>(values.size());
			BigDecimal totalAmount =BigDecimal.ZERO;
			for (DocPmsPoHeaderDetailDo docPmsPoHeaderDetailDo:values) {
				docPmsPoDetails.add(generateDocPmsPoHeaderDetailDo(docPmsPoHeaderDetailDo));
				//参考价格含税
				totalAmount =totalAmount.add(docPmsPoHeaderDetailDo.getPurchaseRealTaxMoney());
			}
			pmsPoHeaderDetailDo.setDocPmsPoDetailDos(docPmsPoDetails);
			pmsPoHeaderDetailDo.setTotalAmount(totalAmount);
			pmsPoHeaderDetailDo.setShopName(shopName);

			returnDocPmsPoHeaderDetailDos.add(pmsPoHeaderDetailDo);
		}
		return returnDocPmsPoHeaderDetailDos;

	}

	public DocPmsPoHeader selectById(Long id){
    		return docPmsPoHeaderManager.getById(id);
	}

	public Integer insert(DocPmsPoHeader docPmsPoHeader){
    		return docPmsPoHeaderManager.insert(docPmsPoHeader);
	}

	public Integer update(DocPmsPoHeader docPmsPoHeader){
    		return  docPmsPoHeaderManager.update(docPmsPoHeader);

	}

	/**
	 *审核接口
	 * @param id
	 * @param state
	 * @return
	 */
	@Override
	public Integer updateStateById(Long id, Integer state) {
		DocPmsPoHeader where = new DocPmsPoHeader();
		where.setId(id);
		DocPmsPoHeader update = new DocPmsPoHeader();
		update.setOrderStatus(state);
		docPmsPoHeaderManager.updateDocPmsPoHeaderOfCommon(where,update);
		DocPmsPoDetail whereDetail =new DocPmsPoDetail();
		whereDetail.setPmsHeaderId(id);
		DocPmsPoDetail updateDetail= new DocPmsPoDetail();
		whereDetail.setOrderDetailStatus(state);
		return  docPmsPoDetailManager.updateDocPmsPoDetailOfCommon(whereDetail,updateDetail);
	}

	public Integer deleteById(Long id){
    		return docPmsPoHeaderManager.deleteById(id);
    }










}
